package main

import (
	"fmt"
	"net/http"
	"strings"
	wancache "wan-cache"

	"github.com/sirupsen/logrus"
)

var mapDataBase = map[string]string{
	"one":   strings.Repeat("hello world ", 1),
	"two":   strings.Repeat("hello world ", 2),
	"three": strings.Repeat("hello world ", 3),
	"four":  strings.Repeat("hello world ", 4),
	"five":  strings.Repeat("hello world ", 5),
}

func main() {
	wancache.NewGroup("student", 1<<20, wancache.GetterFunc(func(key string) ([]byte, error) {
		if v, ok := mapDataBase[key]; ok {
			return []byte(v), nil
		}

		return []byte{}, fmt.Errorf("key is not exist")
	}))

	addr := ":8080"
	s := wancache.NewCacheServer(addr)

	logrus.Fatal(http.ListenAndServe(addr, s))
}
